package com.itheima.demo1;

import java.util.Random;

public class Role {
    private String name;
    private int blood;

    public Role() {
    }

    public Role(String name, int blood, char gender){
        this.name = name;
        this.blood = blood;
    }

        /**
         * 获取
         * @return name
         */
        public String getName () {
        return name;
    }

        /**
         * 设置
         * @param name
         */
        public void setName (String name){
        this.name = name;
    }

        /**
         * 获取
         * @return blood
         */
        public int getBlood () {
        return blood;
    }

        /**
         * 设置
         * @param blood
         */
        public void setBlood ( int blood){
        this.blood = blood;
    }

        //定义一个方法用于攻击
        public void attack (Role role){
        //计算造成的伤害
        Random r = new Random();
        int hurt = r.nextInt(20) + 1;

        //挨揍的人的剩余血量
        int remainBlood = role.getBlood() - hurt;
        //修改剩余血量
        remainBlood = remainBlood < 0 ? 0 : remainBlood;
        role.setBlood(remainBlood);

        //this表示方法的调用者
        System.out.println(this.getName() + "攻击了"
                + role.getName() + "造成了"
                + hurt + "点的伤害,还剩下"
                + remainBlood + "点血");

    }
}

